package org.lzy.shop.util

import android.widget.ImageView
import coil.load
import coil.request.CachePolicy
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import org.lzy.shop.R
import java.io.File

//加载图片，开启缓存
fun setUrl(url: String?,img: ImageView) {
    img.load(url){
        memoryCachePolicy(CachePolicy.ENABLED)
        diskCachePolicy(CachePolicy.ENABLED)
        crossfade(true)
        placeholder(R.mipmap.ic_launcher)
        error(R.mipmap.ic_launcher)
        // 添加内存优化配置
        scale(coil.size.Scale.FIT)   // 设置适当的缩放策略
        allowHardware(false)        // 对于某些情况下禁用硬件加速可以减少内存使用


    }

}

//设置圆角用户头像
fun setFileUrl(url: File?,img: ImageView) {
    img.load(url){
        memoryCachePolicy(CachePolicy.ENABLED)
        diskCachePolicy(CachePolicy.ENABLED)
        crossfade(true)
        placeholder(R.mipmap.ic_launcher)
        error(R.mipmap.ic_launcher)
        size(80,80)
        transformations(CircleCropTransformation())
    }

}

//加载圆形图片
fun setUrlCircle(url: String?,img: ImageView) {
    img.load(url) {
        memoryCachePolicy(CachePolicy.ENABLED)
        diskCachePolicy(CachePolicy.ENABLED)
        crossfade(true)
        placeholder(R.mipmap.ic_launcher)
        error(R.mipmap.ic_launcher)
        transformations(CircleCropTransformation())
  }
}

//加载圆角图片
fun setUrlRound(url: String?,circleNumber: Float,img: ImageView) {
    img.load(url) {
        memoryCachePolicy(CachePolicy.ENABLED)
        diskCachePolicy(CachePolicy.ENABLED)
        crossfade(true)
        placeholder(R.mipmap.ic_launcher)
        error(R.mipmap.ic_launcher)
        transformations(RoundedCornersTransformation(circleNumber))
    }


}

//本地选主题的圆角
fun setUrlLocalRound(localImg: Int?,circleNumber: Float,img: ImageView) {
    img.load(localImg) {
        memoryCachePolicy(CachePolicy.ENABLED)
        diskCachePolicy(CachePolicy.ENABLED)
        crossfade(true)
        transformations(RoundedCornersTransformation(circleNumber))
    }

    // 为不同尺寸的图片添加专用加载方法
    fun setSmallImage(url: String?, img: ImageView) {
        img.load(url) {
            // 针对小图片的优化配置
            memoryCachePolicy(CachePolicy.ENABLED)
            diskCachePolicy(CachePolicy.ENABLED)
            size(200, 200) // 限制最大尺寸
            placeholder(R.mipmap.ic_launcher)
            error(R.mipmap.ic_launcher)
        }
    }
}







